/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import domain.pojo.Apartment;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author lion
 */
public class ApartmentDaoImpl extends ApartmentDao {

    private Session session = null;

    public ApartmentDaoImpl() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    @Override
    public List<Apartment> getApartments() {
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            List<Apartment> apartments = (List<Apartment>) session.createQuery("from Apartment").list();
            transaction.commit();
            return apartments;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Apartment getApartment(int apartmentId) {
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            Apartment apartment = (Apartment) session.get(Apartment.class, apartmentId);
            transaction.commit();
            return apartment;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer insertApartment(Apartment apartment) {
        Transaction transaction = null;
        Integer ApartmentId = null;
        try {
            transaction = session.beginTransaction();
            ApartmentId = (Integer) session.save(apartment);
            transaction.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return ApartmentId;
    }

    @Override
    public boolean updateApartment(Apartment apartment) {
        Transaction transaction = null;
        boolean result = false;
        try {
            transaction = session.beginTransaction();
            session.update(apartment);
            transaction.commit();
            result = true;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean deleteApartment(Apartment apartment) {
        Transaction transaction = null;
        boolean result = false;
        try {
            transaction = session.beginTransaction();
            session.delete(apartment);
            result = true;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean deleteApartment(int ApartmentId) {
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            Apartment apartment = (Apartment) session.get(Apartment.class, ApartmentId);
            return deleteApartment(apartment);
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return false;
    }
}
